﻿<template>
	<div class="deviceGateWay-container">
		<el-dialog v-model="isShowDialog" :width="800" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设备编号" prop="deviceNo">
							<el-input v-model="ruleForm.deviceNo" placeholder="请输入设备编号" maxlength="60" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="机构Id" prop="orgId">
							<el-cascader
								:options="sysOrgTreeData"
								:props="{ checkStrictly: true, emitPath: false, value: 'id', label: 'name' }"
								placeholder="请选择机构Id"
								clearable=""
								class="w100"
								v-model="ruleForm.orgId"
							>
								<template #default="{ node, data }">
									<span>{{ data.name }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="网关名称" prop="gateWayName">
							<el-input v-model="ruleForm.gateWayName" placeholder="请输入网关名称" maxlength="100" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设备地址" prop="deviceAddress">
							<el-input v-model="ruleForm.deviceAddress" placeholder="请输入设备地址" maxlength="200" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设备位置" prop="devicePosition">
							<el-input v-model="ruleForm.devicePosition" placeholder="请输入设备位置" maxlength="50" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设备位置类型" prop="devicePositionType">
							<el-select clearable v-model="ruleForm.devicePositionType" placeholder="请选择设备位置类型">
								<el-option v-for="(item,index) in dl('DevicePositionType')" :key="index" :value="Number(item.value)" :label="`${item.name} (${item.code}) [${item.value}]`"></el-option>
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="序列号" prop="sn">
							<el-input v-model="ruleForm.sn" placeholder="请输入序列号" maxlength="60" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="通讯密码" prop="pwd">
							<el-input v-model="ruleForm.pwd" placeholder="请输入通讯密码" maxlength="60" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="启用状态" prop="deviceEnableStatus">
							<el-select clearable v-model="ruleForm.deviceEnableStatus" placeholder="请选择启用状态">
								<el-option v-for="(item,index) in dl('DeviceEnableStatus')" :key="index" :value="Number(item.value)" :label="`${item.name} (${item.code}) [${item.value}]`"></el-option>
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设备排序" prop="deviceWeight">
							<el-input-number v-model="ruleForm.deviceWeight" placeholder="请输入设备排序" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="云网穿透" prop="deviceFuncCloud">
							<el-switch v-model="ruleForm.deviceFuncCloud" active-text="是" inactive-text="否" />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="激活状态" prop="active">
							<el-select clearable v-model="ruleForm.active" placeholder="请选择激活状态">
								<el-option v-for="(item,index) in dl('Active')" :key="index" :value="Number(item.value)" :label="`${item.name} (${item.code}) [${item.value}]`"></el-option>
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
	import { getDictLabelByVal as dv } from '/@/utils/dict-utils';
  	import { formatDate } from '/@/utils/formatTime';
	import { addDeviceGateWay, updateDeviceGateWay, detailDeviceGateWay } from "/@/api/Device/GateWay/deviceGateWay";
	import { getSysOrgTree } from '/@/api/Device/GateWay/deviceGateWay';
	import { getAPI } from '/@/utils/axios-utils';
	import { SysEnumApi } from '/@/api-services/api';

	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
	//自行添加其他规则
	const rules = ref<FormRules>({
		deviceNo: [{required: true, message: '请输入设备编号！', trigger: 'blur',},],
		orgId: [{required: true, message: '请选择机构Id！', trigger: 'change',},],
		gateWayName: [{required: true, message: '请输入网关名称！', trigger: 'blur',},],
		deviceAddress: [{required: true, message: '请输入设备地址！', trigger: 'blur',},],
		devicePosition: [{required: true, message: '请输入设备位置！', trigger: 'blur',},],
		sn: [{required: true, message: '请输入序列号！', trigger: 'blur',},],
		deviceWeight: [{required: true, message: '请输入设备排序！', trigger: 'blur',},],
	});

	// 页面加载时
	onMounted(() => {

	});

	// 打开弹窗
	const openDialog = async (row: any) => {
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id)
			ruleForm.value = (await detailDeviceGateWay(rowData.id)).data.result;
		else
			ruleForm.value = rowData;
		isShowDialog.value = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		isShowDialog.value = false;
	};

	// 取消
	const cancel = () => {
		isShowDialog.value = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				let values = ruleForm.value;
				if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
					await addDeviceGateWay(values);
				} else {
					await updateDeviceGateWay(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};


	const sysOrgTreeData = ref<any>([]); 
	const getSysOrgTreeData = async () => {
		let list = await getSysOrgTree();
		sysOrgTreeData.value = list.data.result ?? [];
	};
	getSysOrgTreeData();
	




	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>




